package com.kehutong.common;

import com.kehutong.common.dto.DeptDTO;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 获取当前登录员工部门信息
 *
 * @author: liukunlong
 * @date: 2021-04-21 09:28
 */
@Singleton
public class DeptService {

    private static final Logger logger = LoggerFactory.getLogger(DeptService.class);

    @Inject
    private DoveClient doveClient;

    /**
     * 获取部门
     * @param id
     * @return
     * @throws Exception
     */
    public DeptDTO getDept(String id) throws Exception {
        JSONObject resultDepartment = doveClient.post("/basic/department/service/get", (http) -> {
            http.addParam("id",id);
        });
        JSONObject department = resultDepartment.getJSONObject("data");
        DeptDTO deptDTO=new DeptDTO();
        deptDTO.setDepartmentNo(department.getString("id"));
        deptDTO.setDepartmentName(department.getString("name"));

        JSONArray parentIds =department.getJSONArray("parent_ids");
        if(Objects.nonEmpty(parentIds)){
            List<String> parentIdsList = new ArrayList<>(parentIds.size());
            for (int i = 0; i < parentIds.size(); i++) {
                parentIdsList.add(parentIds.getString(i));
            }
            deptDTO.setDepartmentNos(parentIdsList);
        }else{
            deptDTO.setDepartmentNos(Collections.emptyList());
        }
        return deptDTO;
    }
}
